package com.lms.logisticsmanagementsystem.controller;

import com.lms.logisticsmanagementsystem.model.LoginManage;
import com.lms.logisticsmanagementsystem.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /*
    登录页面
     */
    @GetMapping("/login")
    public String login() {
        return "login/login";
    }

    /*
     * 登录检查*/
    @PostMapping("/checkLogin")
    @ResponseBody
    public String checkLogin(@RequestBody LoginManage login_manage,HttpServletRequest request) {
        System.out.println(login_manage);
        boolean result = loginService.checkLogin(login_manage);
        if (result) {
            HttpSession session=request.getSession();
            session.setAttribute("login_username",login_manage.getUsername());
            return "success";
        } else {
            return "error";
        }
    }

    /*
        跳转注册页面
     */
    @GetMapping("/register")
    public String register() {
        return "login/register";
    }

    /*
        上传注册数据
     */
    @PostMapping("/toRegister")
    @ResponseBody
    public String toRegister(@RequestBody LoginManage loginManage) {
        System.out.println(loginManage);//查看上传数据
        //检查注册数据是否已存在，若存在返回true
        boolean flag = loginService.checkRegisterInfo(loginManage);
        System.out.println(flag);
        if (flag) {
            return "0";
        } else {
            //导入用户
            loginService.writeUserInfo(loginManage);
            return "1";
        }
    }
    /*
        跳转至密码修改页面
     */
    @GetMapping("/modifyPasswd")
    public String modifyPasswd(Model model,HttpSession session){
        String username = (String) session.getAttribute("login_username");
        model.addAttribute("username",username);
        return "login/modifyPassword";
    }
    /*
        判断旧密码是否正确
     */
    @PostMapping("/checkOldPassword")
    @ResponseBody
    public String checkOldPassword(@RequestParam("username") String username,@RequestParam("oldPasswd")String oldPasswd,
                                   @RequestParam("newPasswd")String newPasswd){
        //验证旧密码
        boolean checkOldPasswd = loginService.checkOldPassword(username,oldPasswd);
        //如果返回值为true，则更改密码，否则重新修改
        if (checkOldPasswd){
            loginService.updatePassword(username,newPasswd);
            return "success";
        }else {
            return "fail";
        }
    }

    @GetMapping("/exitLogin")
    public String exitLogin(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("username");
        session.invalidate();
        return "redirect:login";
    }
}
